package com.seo.service.proxy.deduc.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.seo.mapper.bill.proxy.ProxyTransactionMapper;
import com.seo.mapper.proxy.ProxyAccountMapper;
import com.seo.pojo.ReturnBody;
import com.seo.service.proxy.sdk.ProxyUserService;
import com.seo.utils.CustomUtil;
import com.seo.utils.DataUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service("proxyDeducService")
public class ProxyDeducServiceImpl{


	@Autowired
	private ProxyTransactionMapper proxyTransactionMapper;

	@Autowired
	private ProxyAccountMapper proxyAccountMapper;

	@Autowired
	private ProxyUserService proxyUserService;




	public ReturnBody saveProxyDeducDetail(String tradeId, Map<String, Object> params) {
		ReturnBody rb = ReturnBody.init();
		try {
			
			if (!params.containsKey("userId")) {
				return rb.error("代理用户不能为空");
			}
			Integer userId = (Integer) params.get("userId");
			if (userId == null || userId <= 0) {
				return rb.error("代理用户不能为空");
			}
			
			log.info("[{}]-查看用户是否存在", tradeId);
			if (!params.containsKey("userName")) {
				log.error("[{}]-用户名不能为空", tradeId);
				return rb.rollback("用户不能为空");
			}

			String userName = (String) params.get("userName");
			rb.run(proxyUserService.getProxyUserByName(userId, userName));
			if (!rb.isSuccess()) {
				return rb;
			}
			Integer proxyUserId = rb.get("proxyUserId", Integer.class);
			if (!params.containsKey("price")) {
				log.error("[{}]-扣费金额不能为空", tradeId);
				return rb.rollback("扣费金额不能为空");
			}

			BigDecimal price = DataUtil.getBigDecimal(params.get("price"));
			if (price.compareTo(BigDecimal.ZERO) <= 0) {
				return rb.error("扣费金额不能小于0");
			}


			params.put("proxyUserId", proxyUserId);

			if (!params.containsKey("remark")) {
				params.put("remark", "手动扣费");
			}

			log.info("[{}]-插入账单明细", tradeId);
			rb.run(saveProxyTransactionFlow(tradeId, params));
			if (!rb.isSuccess()) {
				return rb;
			}
			log.info("[{}]-增加用户余额，插入用户余额明细", tradeId);
			rb.run(saveProxyBalanceDetail(tradeId, rb.get("proxyTf", String.class, Object.class)));
			if (!rb.isSuccess()) {
				return rb;
			}

			return rb.success();
		} catch (Exception e) {
			log.error("异常-{}", e);
			return rb.rollback();
		} finally {
			rb.result();
		}
	}

	public ReturnBody saveProxyTransactionFlow(String tradeId, Map<String, Object> kd) {
		ReturnBody rb = ReturnBody.init();
		Map<String, Object> params = new HashMap<>();
		params.put("userId", kd.get("userId"));
		params.put("proxyUserId", kd.get("proxyUserId"));
		params.put("amount", kd.get("price"));
		params.put("amountState", "02");
		params.put("remark", kd.get("remark"));
		params.put("state", "01");
		params.put("type", "manual_deduc");
		int num = proxyTransactionMapper.saveProxyTransactionFlow(params);
		if (num == 0) {
			log.error("[{}]-插入代理客户账单-失败", tradeId);
			return rb.rollback();
		}
		params.put("code", CustomUtil.orderCode(3, (long) params.get("id"), (int) params.get("proxyUserId")));
		num = proxyTransactionMapper.updateProxyTransactionFlowCode(params);
		if (num == 0) {
			log.error("[{}]-插入代理客户账单-失败", tradeId);
			return rb.rollback();
		}
		log.info("[{}]-插入代理客户账单[{}]-成功", tradeId, params);
		rb.put("proxyTf", params);
		return rb.success();

	}

	public ReturnBody saveProxyBalanceDetail(String tradeId, Map<String, Object> proxyTf) {
		ReturnBody rb = ReturnBody.init();
		Map<String, Object> params = new HashMap<>();
		params.put("proxyUserId", proxyTf.get("proxyUserId"));
		params.put("balance", proxyTf.get("amount"));
		params.put("type", "2");
		int num = proxyAccountMapper.updateProxyUserBalance(params);
		if (num == 0) {
			log.error("[{}]-更新代理用户余额-失败", tradeId);
			return rb.rollback();
		}
		params.put("userId", proxyTf.get("userId"));
		params.put("incomeState", "02");
		params.put("type", "manual_deduc");
		params.put("remark", proxyTf.get("remark"));
		params.put("transactionId", proxyTf.get("id"));
		params.put("state", "01");
		num = proxyAccountMapper.saveProxyBalanceDetail(params);
		if (num == 0) {
			log.error("[{}]-保存代理用户余额明细-失败", tradeId);
			return rb.rollback();
		}
		rb.put("proxyBd", params);
		return rb.success();
	}

}
